<?php

declare(strict_types=1);

namespace App\Model\Ai;

use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\DbConnection\Model\Model;

/**
 * @property int $id
 * @property string $name  模型名称
 * @property string $api_url  模型地址
 * @property int $manufacturer_id  厂商ID
 * @property string $params  参数
 * @property int $sort  排序
 * @property int $status  状态
 * @property \Carbon\Carbon $updated_at  更新时间
 * @property \Carbon\Carbon $created_at  创建时间
 */
class ChatModel extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'chat_model';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = [
        'name',
        'api_url',
        'manufacturer_id',
        'params',
        'sort',
        'status',
        'remark'
    ];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'manufacturer_id' => 'integer', 'sort' => 'integer', 'status' => 'integer', 'updated_at' => 'datetime', 'created_at' => 'datetime'];

    public function setParamsAttribute($value): bool|string
    {
        return $this->attributes['params'] = json_encode($value);
    }

    public function getParamsAttribute($value): array
    {
        return json_decode($value, true);
    }

    /**
     * 关联厂商
     * @return HasOne
     */
    public function manufacturer(): HasOne
    {
        return $this->hasOne(Manufacturer::class, 'id', 'manufacturer_id');
    }

    public function getManufacturerNameAttribute()
    {
        return $this->manufacturer->name ?? '';
    }
}
